package com.example.my_model;

import androidx.room.Room;

import com.example.common.BaseApplication;
import com.example.common.filed.DataActionEntity;
import com.example.common.mvvm.viewmodel.BaseViewModel;
import com.example.my_model.room.UserBean;
import com.example.my_model.room.UserDataBase;
import com.example.my_model.room.UserEntity;
import com.example.network_skd.BaseEntity;

import java.util.HashMap;
import java.util.Map;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class UserViewModel extends BaseViewModel<UserRepository> {

    private UserDataBase db;

    @Override
    protected void initData() {
        db = Room.databaseBuilder(BaseApplication.getInstance(), UserDataBase.class, "user.db").build();

    }



    @Override
    protected void onResult(BaseEntity baseEntity) {
        if (baseEntity instanceof UserBean){
            UserBean userBean = (UserBean) baseEntity;
            if (userBean.message.equals("用户已登录")){
                DataActionEntity dataActionEntity = new DataActionEntity();
                dataActionEntity.action = "DATA";
                Map<String, Object> map = new HashMap<>();
                map.put("VIDEODATA", userBean.data);
                dataActionEntity.data = map;
                vmData.post(dataActionEntity);
            }

        }
    }

    public void getUser(){
        repository.getUser(db.userDao())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(this);
    }
}
